function debug(model, method, ...data) {
    console.group("【" + model + "】" + method)
    console.log(...data)
    console.groupEnd()
}

function createComponent(cmpName, props = {}, events = {}) {
    const Comp = Vue.component(cmpName)
    const CompExtend = Vue.extend(Comp)
    const comp = new CompExtend({ propsData: props }).$mount()
    Object.keys(events).forEach(key => {
        comp.$on(key, events[key])
    })
    document.body.appendChild(comp.$el)
    comp._removeFromDocument = () => {
        document.body.removeChild(comp.$el)
        comp.$destroy()
    }
    return comp
}

function fetch(url, data, cb, options = {}) {
    options.notice = false
    return post(url, data, cb, options)
}

function post(url, data, cb, options={}) {
    $Message = window.$Message || {
        success: () => {},
        error: () => {},
    }
    $Modal = window.$Modal || {
        confirm: () => {},
    }
    axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
    debug('post', url, data)
    const onUploadProgress = options.progress ? options.progress : () => {}
    axios.post(url, data, {
        onUploadProgress,
    })
    .then(function (response) {
        const result = response.data
        if(result.code == 0) {
            if(options.notice !== false) {
                $Message.success({ content: '请求成功' })
            }
            cb(true, result.data)
            return
        }
        $Message.error({ content: result.message || '请求失败' })
        cb(false, null)
        console.error(result);
    })
    .catch(function (error) {
        const code = error.response && error.response.status
        if(code == 404) {
            $Message.error({ content: '请求路径错误 404' })
        } else if(code == 500) {
            $Message.error({ content: '服务器错误 500' })
        } else if(code == 401) {
            $Modal.confirm({
                title: '登录失效',
                content: '请重新登录',
                onOk: () => {
                    window.location.reload(true)
                }
            })
        } else {
            console.error(error); 
        }
        cb(false, null)
    });
}

function uploadServer(url, file, progress, cb, options = {}) {
    const fileName = options.name ? options.name : 'file'
    let data = new FormData()
    data.append(fileName, file)
    post(url, data, cb, {
        progress: function(progressEvent){
            let precnet = (progressEvent.loaded / progressEvent.total * 100 | 0)
            progress(precnet)
        }
    })
}

/**
 * https://www.npmjs.com/package/exceljs
 */
async function exportTabelExcel(columns, list, options = {}) {
    const filename = options.filename ? options.filename : ((Date.parse(new Date()) / 1000) + '.xlsx')
    const workbook = new ExcelJS.Workbook();
    const worksheet = workbook.addWorksheet('SHEET', {})
    const workSheetColumns = []
    columns.map(col => {
        if(col.type == 'selection') return
        workSheetColumns.push({
            header: col.title,
            key: col.text_key || col.key,
            width: col.width || 20
        })
    })
    worksheet.columns = workSheetColumns
    list.map(row => {
        worksheet.addRow(row)
    })
    const data = await workbook.xlsx.writeBuffer();
    const bolb = new Blob([data]);
    saveAs(bolb, filename)
}
